//
//  BorrowHuanKuanPlanVC.m
//  BrushFace
//
//  Created by timer_q on 15/12/11.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "BorrowHuanKuanPlanVC.h"
#import "HuanKuanFooterView.h"
#import "HuanKuanHeaderView.h"
#import "HuanKuanPlanCell.h"
#import "NavHelper.h"
#import "BorrowScenceModel.h"
#import "IWillHelper.h"

@interface BorrowHuanKuanPlanVC ()<UITableViewDataSource,UITableViewDelegate>
{
    HuanKuanFooterView *huanKuanFooter;
}
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) BorrowScenceModel *requestModel;
@end

@implementation BorrowHuanKuanPlanVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self UIConfig];
    [self dataInit];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - CustomMethods
- (void)UIConfig{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, SCREEN_WIDTH, SCREEN_HEIGHT-74) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    HuanKuanHeaderView *huanKuanHeader = [HuanKuanHeaderView loadBundleNib];
    huanKuanFooter = [HuanKuanFooterView loadBundleNib];
    huanKuanHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    huanKuanFooter.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
    _tableView.tableHeaderView = huanKuanHeader;
    _tableView.tableFooterView = huanKuanFooter;
    
#pragma mark -----xuxuejiao
    
    //改了一下frame，这种上下都有mark的整个copy就行
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 120, 50)];
    [button setTitle:@"还款方式说明" forState:UIControlStateNormal];
    button.titleLabel.font=[UIFont systemFontOfSize:14];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(gotoBorrowHuanKuanInstructionsVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar setRightView:button];
}
#pragma mark---------------------------
- (void)dataInit{
    [self.navBar setTitle:@"还款计划"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];

    _requestModel = [BorrowScenceModel SceneModel];
    [_requestModel hope_CreaterePayPlanCB:^(id output){
        NSArray *array = [output objectForKey:@"data"];
        CGFloat amount=0;
        CGFloat lixi=0;
        for (NSDictionary *subDict in array) {
            amount += [[NSString stringWithFormat:@"%@",subDict[@"monthcapital"]] floatValue];
            lixi += [[NSString stringWithFormat:@"%@",subDict[@"monthinterest"]] floatValue];
        }
        
        huanKuanFooter.BLabel.text = [NSString stringWithFormat:@"应还款总额: %.2f (含本金 ¥ %.2f、利息 ¥ %.2f)",amount+lixi,amount,lixi];
        
        [_tableView reloadData];
    }];
    
}
#pragma mark - tableView Delegate and DataSource 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _requestModel.planList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"HuanKuanPlanCell";
    HuanKuanPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [HuanKuanPlanCell loadBundleNib];
    }
     NSDictionary *dict = [_requestModel.planList objectAtIndex:indexPath.row];
    cell.dateLabel.text = [NSString stringWithFormat:@"%@",[dict objectForKey:@"paymentdate"]];
#pragma mark------------qiweipeng
    cell.lixiLabel.text = [NSString stringWithFormat:@"%.2f",[[dict objectForKey:@"monthinterest"] floatValue]];
    cell.benjinLabel.text = [NSString stringWithFormat:@"%.2f",[[dict objectForKey:@"monthcapital"] floatValue]];
#pragma mark------------
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark - action
- (void)gotoBorrowHuanKuanInstructionsVC:(UIButton *)sender{
    [NavHelper gotoBorrowHuanKuanInstructionsVC];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
